package com.ruoyi.project.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.CollegePaperAnswerDetailMapper;
import com.ruoyi.project.system.domain.CollegePaperAnswerDetail;
import com.ruoyi.project.system.service.ICollegePaperAnswerDetailService;

/**
 * 答题详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-19
 */
@Service
public class CollegePaperAnswerDetailServiceImpl implements ICollegePaperAnswerDetailService 
{
    @Autowired
    private CollegePaperAnswerDetailMapper collegePaperAnswerDetailMapper;

    /**
     * 查询答题详情
     * 
     * @param id 答题详情ID
     * @return 答题详情
     */
    @Override
    public CollegePaperAnswerDetail selectCollegePaperAnswerDetailById(Long id)
    {
        return collegePaperAnswerDetailMapper.selectCollegePaperAnswerDetailById(id);
    }

    /**
     * 查询学校考试结果列表
     * 
     * @param collegePaperAnswerDetail 答题详情
     * @return 答题详情
     */
    @Override
    public List<Map<String,Object>> selectCollegePaperAnswerDetailList(CollegePaperAnswerDetail collegePaperAnswerDetail)
    {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if(loginUser.getUser().getUserId()!=1){
            collegePaperAnswerDetail.setCollegePaperAnswerId(loginUser.getCollegeStaff().getCollegeId());
        }
        return collegePaperAnswerDetailMapper.selectDetailList(collegePaperAnswerDetail);
    }

    /**
     * 新增答题详情
     * 
     * @param collegePaperAnswerDetail 答题详情
     * @return 结果
     */
    @Override
    public int insertCollegePaperAnswerDetail(CollegePaperAnswerDetail collegePaperAnswerDetail)
    {

        CollegePaperAnswerDetail cp = new CollegePaperAnswerDetail();
        cp.setCollegePaperAnswerId(collegePaperAnswerDetail.getCollegePaperAnswerId());
        cp.setQuestionStoreId(collegePaperAnswerDetail.getQuestionStoreId());
        List<CollegePaperAnswerDetail> collegePaperAnswerDetails = collegePaperAnswerDetailMapper.selectCollegePaperAnswerDetailList(cp);
        if(collegePaperAnswerDetails.size()==0){
            return collegePaperAnswerDetailMapper.insertCollegePaperAnswerDetail(collegePaperAnswerDetail);
        }else{
            collegePaperAnswerDetail.setId(collegePaperAnswerDetails.get(0).getId());
            return collegePaperAnswerDetailMapper.updateCollegePaperAnswerDetail(collegePaperAnswerDetail);
        }


    }

    /**
     * 修改答题详情
     * 
     * @param collegePaperAnswerDetail 答题详情
     * @return 结果
     */
    @Override
    public int updateCollegePaperAnswerDetail(CollegePaperAnswerDetail collegePaperAnswerDetail)
    {
        return collegePaperAnswerDetailMapper.updateCollegePaperAnswerDetail(collegePaperAnswerDetail);
    }

    /**
     * 批量删除答题详情
     * 
     * @param ids 需要删除的答题详情ID
     * @return 结果
     */
    @Override
    public int deleteCollegePaperAnswerDetailByIds(Long[] ids)
    {
        return collegePaperAnswerDetailMapper.deleteCollegePaperAnswerDetailByIds(ids);
    }

    /**
     * 删除答题详情信息
     * 
     * @param id 答题详情ID
     * @return 结果
     */
    @Override
    public int deleteCollegePaperAnswerDetailById(Long id)
    {
        return collegePaperAnswerDetailMapper.deleteCollegePaperAnswerDetailById(id);
    }

    @Override
    public List<Map<String, Object>> selectListinfo(CollegePaperAnswerDetail collegePaperAnswerDetail) {

        return collegePaperAnswerDetailMapper.selectListinfo(collegePaperAnswerDetail);
    }
}
